package link;

public class FlattenAMultilevelDoublyLinkedList430 {
    class Node {
        public int val;
        public Node prev;
        public Node next;
        public Node child;
    }
    public Node flatten(Node head) {
        Node temp = head;
        while (temp!=null){
            if(temp.child!=null){
                Node node = flatten(temp.child);
//                temp.child=null;
                Node tt = temp.next;
                temp.next = node;
                node.prev = temp;
                while (temp.next!=null){
                    temp = temp.next;
                }
                temp.next = tt;
                if(tt!=null) {
                    tt.prev = temp;
                }
            }
            temp = temp.next;
        }
        return head;
    }
}
